package com.snake.action;

import com.opensymphony.xwork2.ActionSupport;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 服务端测试，用于网络连接
 */
public class UserAction extends ActionSupport{
		private String username;
		private String password;
		
		public File mPhoto;
		public String mPhtotFileName;
		
		public String getUsername() {
			return username;
		}
		public void setUsername(String username) {
			this.username = username;
		}
		public String getPassword() {
			return password;
		}
		public void setPassword(String password) {
			this.password = password;
		}
		
		public String uploadForm() throws IOException{
			
			if(mPhoto==null){
				System.out.println(mPhtotFileName+" is null .");
			}
			String dir = ServletActionContext.getServletContext().getRealPath("files");
			File dirs = new File(dir);
			if(!dirs.exists()){
				dirs.mkdirs();
			}
			File file = new File(dir,"pic2.jpg");
			FileUtils.copyFile(mPhoto, file);
			System.out.println(file.getAbsolutePath());
			System.out.println(username+" , "+password);
			HttpServletResponse response = ServletActionContext.getResponse();
			PrintWriter writer = response.getWriter();
			writer.write("post form  success!");
			writer.flush();
			return null;
		}
		
		
		
		public String postFile() throws IOException{
			System.out.println("postFile");
			HttpServletRequest request = ServletActionContext.getRequest();
			ServletInputStream is = request.getInputStream();
			String dir = ServletActionContext.getServletContext().getRealPath("files");
			File dirs = new File(dir);
			if(!dirs.exists()){
				dirs.mkdirs();
			}
			File file = new File(dir,"pic1.jpg");
			System.out.println(file.getAbsolutePath());
			FileOutputStream fos = new FileOutputStream(file);
			int len = 0;
			byte[] buf = new byte[1024];
			
			while((len=is.read(buf))!= -1){
				fos.write(buf, 0, len);
			}
			is.close();
			fos.close();
			HttpServletResponse response = ServletActionContext.getResponse();
			PrintWriter writer = response.getWriter();
			writer.write("post file  success!");
			writer.flush();
			
			return null;
		}
		
		public String postString() throws IOException{
			HttpServletRequest request = ServletActionContext.getRequest();
			System.out.println("sessionId : "+request.getSession().getId());//session在访问tomcat服务器HttpServletRequest的getSession(true)的时候创建，tomcat的ManagerBase类提供创建sessionid的方法：随机数+时间+jvmid；
			ServletInputStream is = request.getInputStream();
			int len = 0;
			byte[] buf = new byte[1024];
			StringBuilder sb = new StringBuilder();
			while((len=is.read(buf))!= -1){
				sb.append(new String(buf,0,len));
			}
			System.out.println(sb.toString());
			is.close();
			HttpServletResponse response = ServletActionContext.getResponse();
			PrintWriter writer = response.getWriter();
			writer.write("post String  success!");
			writer.flush();
			
			return null;
		}
		
		
		public String login() throws IOException{
			HttpServletRequest request = ServletActionContext.getRequest();
			
			System.out.println("sessionId : "+request.getSession().getId());
			System.out.println(username+" , "+password);
			HttpServletResponse response = ServletActionContext.getResponse();
			PrintWriter writer = response.getWriter();
			writer.write("login success!");
			writer.flush();
			return null;
		}
}
